package com.zit.java.service.role;

import com.zit.java.dao.SysRolesMapper;
import com.zit.java.dao.SysUserMapper;
import com.zit.java.pojo.SysRoles;
import com.zit.java.pojo.SysUser;
import com.zit.java.pojo.ZTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private SysRolesMapper dao;

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public List<SysRoles> queryAllRole() {

        //使用插件进行分页查询
//        PageHelper.startPage(start,limit);
        List<SysRoles> pageList = dao.selectAllRoles();

        return pageList;
    }

    @Override
    public int saveRole(SysRoles SysRoles) {
        //使
        int rst = dao.insertSelective(SysRoles);
        return rst;
    }

    @Override
    public int delRole(String roleId) {
        int id = dao.deleteByPrimaryKey(Integer.parseInt(roleId));
        return id;
    }

    @Override
    public int updateRole(SysRoles sysRoles) {

        return dao.updateByPrimaryKey(sysRoles);
    }

    @Override
    public int updateStatus(Integer roleId, Integer roleStatus) {
        SysRoles sysRoles = new SysRoles();
        sysRoles.setRoleId(roleId);
        sysRoles.setRoleStatus(roleStatus);
        int rst = dao.updateByPrimaryKeySelective(sysRoles);
        return rst;
    }

    @Override
    public List<SysRoles> showRoleId() {

        List<SysRoles> rstLsit = dao.showRoleId();
        return rstLsit;
    }


    /***
     * 创建用户管理树
     * @param menuId
     * @param roleId
     * @return
     */
    @Override
    public List<ZTreeNode> createTree(String menuId, String roleId) {
        //1.查询权限项目信息
        List<Map<String, Object>> listRights = dao.queryRightsItem(Integer.parseInt(menuId), roleId);
        System.out.println("查询出的result:" + listRights);
        List<ZTreeNode> listZTree = new ArrayList<>();

        for (Map<String, Object> map : listRights) {
            ZTreeNode zTreeNode = new ZTreeNode();

            zTreeNode.setId(map.get("rights_id").toString());
            System.out.println("rightsId:" + map.get("rights_id"));

            zTreeNode.setName((String) map.get("title"));
            System.out.println("title:" + map.get("title"));
            //判断是否有子节点
            if (Integer.parseInt(map.get("childNum").toString()) > 0) {
                //展开父节点
                zTreeNode.setOpen(true);
                //递归查询子节点
                List<ZTreeNode> childNode = createTree(map.get("menu_id").toString(), roleId);
                zTreeNode.setChildren(childNode);
            }

            List<Map<String, Object>> queryButnRights = dao.queryBtnItem(roleId, map.get("menu_id").toString());

            //偷梁换柱,对上面的queryButnRights进行处理
            List<Map<String, Object>> newQueryButnRights = queryButnRights.stream().peek(
                    m -> {
                        if (Integer.parseInt(m.get("isChecked").toString()) > 0) {
                            m.put("isChecked", "checked");
                        }
                    }
            ).collect(Collectors.toList());
            //设置前端页面显示的按钮
            zTreeNode.setModel(newQueryButnRights);

            //设置父节点是否被选中
            zTreeNode.setChecked(Integer.parseInt(map.get("isChecked").toString()) > 0);
            listZTree.add(zTreeNode);

        }

        return listZTree;
    }

    @Override
    public int saveRoleRights(String roleId, String[] rightsIds) {
        //删除数据库原有的角色权限信息
        dao.deletRoleId(roleId);
        //添加勾选的角色权限信息
        int rst = dao.saveRoleRights(roleId, rightsIds);
        return rst;
    }

    @Override
    public SysUser selectOneUser(String id) {
        SysUser sysUser = userMapper.selectByPrimaryKey(Integer.parseInt(id));
        return sysUser;
    }

    @Override
    public SysRoles queryRoleByRoleId(String roleId) {
        SysRoles sysRoles = dao.selectByPrimaryKey(Integer.valueOf(roleId));
        return sysRoles;
    }

    @Override
    public int updateRoleByRoleId(SysRoles sysRoles) {
        int i = dao.updateByPrimaryKeySelective(sysRoles);
        return i;
    }


}
